package com.teamit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.teamit.entity.Contact;
import com.teamit.entity.ContactNote;
import com.teamit.service.ContactNoteService;
import com.teamit.util.db.DBHelp;
import com.teamit.util.db.RowMapper;

public class ContactDao {
	private DBHelp<Contact> db=new DBHelp<Contact>();
	public void update(String id,Contact contact){
		String sql="update t_contact set `name`=?,companyname=?,mobile=?,tel=?,email=?,address=?,pagehome=?,website=?,weibo=?,content=? where id=?";
		db.executeSQL(sql, contact.getName(),contact.getCompanyname(),contact.getMobile(),contact.getTel(),contact.getEmail(),contact.getAddress(),contact.getPageHome(),contact.getWebsite(),contact.getWeibo(),contact.getContent(),id);
	}
	public void del(String id){
		String sql="delete from t_contact where id=?";
		db.executeSQL(sql, id);
	}
	public Contact findById(String id){
		String sql="SELECT id,`name`,companyname,mobile,tel,email,address,pagehome,website,weibo,content,createtime FROM t_contact WHERE id=? order by createtime desc";
		return db.executeQueryToObject(sql, new RowMapper<Contact>(){

			public Contact mapRow(ResultSet rs) throws SQLException {
				Contact contact=new Contact();
				contact.setId(rs.getString("id"));
				contact.setName(rs.getString("name"));
				contact.setCompanyname(rs.getString("companyname"));
				contact.setMobile(rs.getString("mobile"));
				contact.setTel(rs.getString("tel"));
				contact.setEmail(rs.getString("email"));
				contact.setAddress(rs.getString("address"));
				contact.setPageHome(rs.getString("pagehome"));
				contact.setWebsite(rs.getString("website"));
				contact.setWeibo(rs.getString("weibo"));
				contact.setContent(rs.getString("content"));
				contact.setCreatetime(rs.getString("createtime"));
				ContactNoteService cns=new ContactNoteService();
				List<ContactNote> ctnList=cns.findById(rs.getString("id"));
				contact.setCnList(ctnList);
				return contact;
			}}, id);
	}
	public void save(Contact contact,String projectId){
		String sql="INSERT INTO t_contact(id,`name`,companyname,mobile,tel,email,address,pagehome,website,weibo,content,project_id) VALUES(?,?,?,?,?,?,?,?,?,?,?,?)";
		db.executeSQL(sql, contact.getId(),contact.getName(),contact.getCompanyname(),contact.getMobile(),contact.getTel(),contact.getEmail(),contact.getAddress(),contact.getPageHome(),contact.getWebsite(),contact.getWeibo(),contact.getContent(),projectId);
	}
	public List<Contact> findByProjectId(String projectId){
		String sql="SELECT id,`name`,companyname,mobile,tel,email,address,pagehome,website,weibo,content,createtime FROM t_contact WHERE project_id=? order by createtime desc";
		return db.executeQueryToList(sql, new MapperContact(), projectId);
	}
	private class MapperContact implements RowMapper<Contact>{
		public Contact mapRow(ResultSet rs) throws SQLException {
			Contact contact=new Contact();
			contact.setId(rs.getString("id"));
			contact.setName(rs.getString("name"));
			contact.setCompanyname(rs.getString("companyname"));
			contact.setMobile(rs.getString("mobile"));
			contact.setTel(rs.getString("tel"));
			contact.setEmail(rs.getString("email"));
			contact.setAddress(rs.getString("address"));
			contact.setPageHome(rs.getString("pagehome"));
			contact.setWebsite(rs.getString("website"));
			contact.setWeibo(rs.getString("weibo"));
			contact.setContent(rs.getString("content"));
			contact.setCreatetime(rs.getString("createtime"));
			return contact;
			
		}}
}
